home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998…eptember: Technology Seed / September 98 ADC Seed CD.toast / Open Transport 1.3 / Open Transport SDK / Open Tpt Protocol Developer / Samples / Configurators / TMP_RS_Config.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-24  |  6.0 KB  |  219 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TMP_RS_Config.c
  3.  
  4.     Contains:    The residend portion of the a configurator
  5.     
  6.                 This code is not intended to implement an actual 
  7.                 working configurator.  It comes with no warranty,
  8.                 express or implied, as to it's suitability for your
  9.                 own configuration implementation.
  10. */
  11.  
  12. #ifndef __TMP_CONFIG__
  13. #include "TMP_Config.h"
  14. #endif
  15. #ifndef __OTSHAREDLIBS__
  16. #include "OTSharedLibs.h"
  17. #endif
  18.  
  19. #if TMPCONFIG_USES_ASLM
  20. #include <LibraryManager.h>
  21. #endif
  22.  
  23. /*******************************************************************************
  24. ** Forward declarations
  25. ********************************************************************************/
  26.  
  27. static void ScheduleUnload();
  28. static pascal void DoUnload(void*);
  29.  
  30. OSStatus OTSetupConfigurator(OTCanConfigureProcPtr* ccProc, 
  31.                              OTCreateConfiguratorProcPtr* ccProc2,
  32.                              UInt8* typePtr);
  33.  
  34. /*******************************************************************************
  35. ** Some Globals
  36. ********************************************************************************/
  37.  
  38. #if TMPCONFIG_USES_CFM
  39.     static UInt32    gMyCFMID;
  40. #endif
  41.  
  42. /*
  43.  * Flag that an unload is scheduled
  44.  */
  45. Boolean            gUnloadScheduled    = false;
  46. /*
  47.  * This is data shared between us and our "controller".
  48.  */
  49. MySharedStruct    gShared                = { 0 };
  50. /*
  51.  * List of protocols
  52.  */
  53. static char* gList[] = { kProtocolList, 0 };
  54.  
  55. /*******************************************************************************
  56. ** ScheduleUnload and DoUnload
  57. ********************************************************************************/
  58.  
  59. static void ScheduleUnload()
  60. {
  61.     gUnloadScheduled = true;
  62.     OTScheduleSystemTask(gShared.systemTask);
  63. }
  64.  
  65. static pascal void DoUnload(void* dummy)
  66. {
  67.     #pragma unused(dummy)
  68.     
  69.     if ( gUnloadScheduled )
  70.     {
  71.         gUnloadScheduled = false;
  72.         /*
  73.          * Note that we destroy the system task, but we let the controller
  74.          * library create it.  That's because we aren't a client of Open Transport
  75.          * (i.e. we didn't call InitOpenTransport), so we can't do anything that 
  76.          * will allocate memory or create endpoints or we'll crash. 
  77.          * We can, however, free memory, since that doesn't require
  78.          * Open Transport to know about us (as long as Open Transport is really
  79.          * loaded, which we know to be true, because our main code DID do an
  80.          * InitOpenTransport, and will not do a CloseOpenTransport until we
  81.          * unload them in the next couple of lines).
  82.          */
  83.         OTDestroySystemTask(gShared.systemTask);
  84.         gShared.systemTask = 0;
  85.  
  86.     #if TMPCONFIG_USES_CFM
  87.         OTReleaseCFMConnection(&gMyCFMID);
  88.     #else
  89.         OTUnloadASLMLibrary(kTMPConfigASLMID);
  90.         /*
  91.          * This is an ASLM function to force the libraries to unload "right now".
  92.          * Otherwise, they will not unload until the next system task.
  93.          */
  94.         UnloadUnusedLibraries();
  95.     #endif
  96.     }
  97. }
  98.  
  99. /*******************************************************************************
  100. ** TMP_CanConfigure
  101. **
  102. ** This function determines if we can configure the supplied configuration.
  103. ** It normally looks at the top name in the configuration to see if it's one of
  104. ** the protocols or drivers that we handle.
  105. ********************************************************************************/
  106.  
  107. static Boolean TMP_CanConfigure(OTConfiguration* cfig, UInt32 pass)
  108. {
  109.     #pragma unused(pass)
  110.     
  111.     const char*    name = OTCfigGetProviderName(cfig);
  112.     SInt16        index = -1;
  113.     while ( gList[++index] != NULL )
  114.     {
  115.         if ( OTStrEqual(name, gList[index]) )
  116.             return true;
  117.     }
  118.     return false;
  119. }
  120.  
  121. /*******************************************************************************
  122. ** TMP_CreateConfigurator
  123. **
  124. ** This function loads and creates our configurator.  It is called by the
  125. ** system when someone wants the configurator, and we're not already loaded.
  126. ********************************************************************************/
  127.  
  128. static OSStatus TMP_CreateConfigurator(TOTConfigurator** cfigPtr)
  129. {
  130.     ProcPtr        proc;
  131.     OSStatus    err;
  132. #if TMPCONFIG_USES_ASLM
  133.     OSErr        osErr;
  134. #endif
  135.     /*
  136.      * If we're scheduled to unload, do it so we get a fresh copy
  137.      */
  138.     if ( gUnloadScheduled )
  139.     {
  140.         DoUnload(NULL);
  141.     }
  142.     /*
  143.      * Set up the vectors that our configurator code needs to request itself
  144.      * to be unloaded.  Unfortunately, the configurator cannot unload itself
  145.      * or a crash might occur, since under certain conditions using CFM and
  146.      * VM on, the code memory becomes unusable immediately upon calling
  147.      * CloseConnection.
  148.      */
  149.     gShared.scheduleUnloadProc    = &ScheduleUnload;
  150.     gShared.unloadProc            = &DoUnload;
  151.     /*
  152.      * Here, we load in our shared library and invoke it's 'OTCreateMyConfigurator'
  153.      * function that we exported.
  154.      */
  155. #if TMPCONFIG_USES_CFM
  156.  
  157.     proc = (ProcPtr)OTGetCFMPointer(kTMPConfigCFMMID, "OTCreateMyConfigurator", &gMyCFMID, 
  158.                                     kOTLoadACopy | kOTGetCodeSymbol);
  159.     if ( proc == 0 )
  160.         return kENXIOErr;
  161.  
  162.     err = (*(CreateMyConfigProcPtr)proc)(&gShared);
  163.     
  164. #else
  165.     
  166.     osErr = OTLoadASLMLibrary(kTMPConfigASLMID);
  167.     if ( osErr != 0 )
  168.         return osErr;
  169.         
  170.     proc = (ProcPtr)GetFunctionPointer(kTMPConfigASLMID, 
  171.                                        "OTCreateMyConfigurator", &osErr);
  172.     if ( proc == 0 )
  173.     {
  174.         err = osErr;
  175.     }
  176.     else
  177.     {
  178.         err = (*(CreateMyConfigProcPtr)proc)(&gShared);
  179.     }
  180.     
  181. #endif
  182.     /*
  183.      * If an error occurred, unload the configurator code.  Otherwise, copy
  184.      * the configurator pointer to the client.
  185.      */
  186.     if ( err != kOTNoError )
  187.     {
  188.         gUnloadScheduled = true;    /* Lie, so DoUnload will cause the unload */
  189.         DoUnload(NULL);
  190.     }
  191.     else
  192.     {
  193.         *cfigPtr = gShared.cfig;    /* Save the pointer to our controller */
  194.     }
  195.     return err;
  196. }
  197.  
  198. /*******************************************************************************
  199. ** OTSetupConfigurator
  200. **
  201. ** The system calls this function when the system first comes up to get our
  202. ** entry points for configuring.
  203. ********************************************************************************/
  204.  
  205. OSStatus OTSetupConfigurator(OTCanConfigureProcPtr* ccProc, 
  206.                              OTCreateConfiguratorProcPtr* ccProc2,
  207.                              UInt8* typePtr)
  208. {
  209.     /*
  210.      * Select the typePtr that's appropriate.
  211.      */
  212.     *typePtr    = kOTProtocolFamilyConfigurator;
  213.     *typePtr    = kOTLinkDriverConfigurator;
  214.  
  215.     *ccProc        = &TMP_CanConfigure;
  216.     *ccProc2    = &TMP_CreateConfigurator;
  217.     return kOTNoError;
  218. }
  219.